import Vue from 'vue';
import App from './App.vue';
import store from './vuex/store.js';
import router from './router';
import ElementUI from 'element-ui';
import VueI18n from 'vue-i18n';
import { messages } from './components/common/i18n';
import 'element-ui/lib/theme-chalk/index.css'; // 默认主题
// import './assets/css/theme-green/index.css'; // 浅绿色主题
import './assets/css/icon.css';
import './components/common/directives';
import 'babel-polyfill';
import {post,get} from './utils/http';
import attr from './router/peimission.js';
import objectArraySort from './components/common/common.js';
import vueJsonp from 'vue-jsonp'
Vue.use(vueJsonp)
//定义全局变量

Vue.prototype.$post = post;
Vue.prototype.$get = get;
Vue.prototype.$objectArraySort = objectArraySort;
Vue.prototype.$attr = attr;
Vue.config.productionTip = false;
Vue.use(VueI18n);
Vue.use(ElementUI, {
    size: 'small'
});
const i18n = new VueI18n({
    locale: 'zh',
    messages
});

let lbl=0;
//使用钩子函数对路由进行权限跳转
router.beforeEach((to, from, next) => {
    document.title = `${to.meta.title} | vue-manage-system`;
    var role = sessionStorage.getItem('token');
    // var detail='';
    next(); 
    if(to.path=='/login'){
        sessionStorage.setItem('token','');
        localStorage.setItem('detail','');
        store.commit('dellist');
        lbl=0;
    }
    if(!role  && to.path !== '/login'){
        next('/login');
    }else{
        // store.commit('dellist')
        if(lbl<1 && to.path !== '/login'){
            post('admin/login/jurisdiction').then( val =>{
                lbl++;
                let list=[];
                sessionStorage.setItem('permissmion',JSON.stringify(val));
                if(val.code != 200 && to.path !== '/login'){
                    role = false;
                    next('/login');
                }else{
                    let data=[];
                    if(val.data.data){
                    let sort = val.data.data.sort(objectArraySort('int_sort'))
                    sort.forEach((item,index)=>{
                        if(item.type=='menu'){
                            if(item.url){
                                //添加路由数组
                                data.push({
                                    path:item.url,
                                    page:item.percode,
                                    title:item.name
                                })
                            }
                            // if(!store.state.permission_list.length){
                            let place = item.sortstring.indexOf('-')
                            let place1=0;
                            let place2=0;
                            let split = item.sortstring.split('-').length-1;
                            // console.log(split)
                            // console.log(split)
                            if(place===-1){
                                list.push({
                                    biaoji: item.sortstring,
                                    icon:  item.icon,
                                    index: item.url ? item.url.substr(1) : item.sortstring,
                                    title: item.name
                                })
    
                            }else if(split === 1){
                                place1 = item.sortstring.substring(0,place)
                                list.forEach( its =>{
                                    if(its.biaoji==place1){
                                        if(!its.subs){
                                            its.subs=[];
                                        }
                                        its.subs.push({
                                            biaoji:item.sortstring,
                                            index: item.url ? item.url.substr(1) : item.sortstring,
                                            title: item.name
                                        })
                                    }
                                })
                            }else if(split === 2){
                                place2 = item.sortstring.substring(0,item.sortstring.indexOf('-',place+1))
                                list.forEach( it =>{
                                    if(it.subs){          
                                    it.subs.forEach(subs =>{
                                        if(subs.biaoji==place2){
                                            if(!subs.subs){
                                                subs.subs=[];
                                            }
                                            subs.subs.push({
                                                biaoji:item.sortstring,
                                                index: item.url ? item.url.substr(1) : item.sortstring,
                                                title: item.name
                                            })
                                    }
                                })
                                }
    
                                })
                            } 
                        // }
                        }
                    })
                    store.commit('addlist',list);
                    router.addRoutes(attr(data));
                    next({...to,replate:true})
                    lbl=0;
                }
                }
            })
        // }
        // if(to.path=='/login'){
        //     store.commit('dellist')
        }

    }
});

    
new Vue({
    router,
    store,
    i18n,
    render: h => h(App)
}).$mount('#app');
